<?php
namespace app\admin\controller;
use app\lib\exception\LoginException;
use app\lib\exception\SuccessMessage;
use app\lib\exception\UploadImgException;
use think\Request;
use app\admin\model\Article as ArtModel;

class Article extends Base {
    public function Index() {
        return view();
    }

    //添加图片模板
    public function addImg() {
        if(Request::instance()->post()) {
            $data = Request::instance()->post();
            $data['author_id'] = session('admin_id');
            $data['author_name'] = session('admin_name');
            $data['type'] = 2;
            $data['content'] = "<img width='100%' src='".$data['book_img']."'>";
            $res = ArtModel::create($data, true);
            $this->returnAjax($res);
        }

        return view();
    }

    //添加文字模板
    public function addWriting() {
        if(Request::instance()->post()) {
            $data = Request::instance()->post();
            $data['author_id'] = session('admin_id');
            $data['author_name'] = session('admin_name');
            $data['type'] = 3;
            $data['content'] = "<p>".$data['content']."</p>";
            $res = ArtModel::create($data, true);
            $this->returnAjax($res);
        }


        return view();
    }

    //添加图文模板
    public function addBoth() {
        if(Request::instance()->post()) {
            $data = Request::instance()->post();
            $data['author_id'] = session('admin_id');
            $data['author_name'] = session('admin_name');
            $data['type'] = 1;
            //判断是否是更新操作
            if(isset($data['edit_id']) && !empty($data['edit_id'])) {
                $ret = ArtModel::update($data, array('id' => $data['edit_id']), 'title,book_img,content,update_time');
                if($ret) {
                    throw new SuccessMessage([
                        'msg' => '更新成功'
                    ]);
                }else {
                    throw new LoginException([
                        'msg' => '更新失败'
                    ]);
                }
            }

            $res = ArtModel::create($data, true);
            if($res) {
                throw new SuccessMessage([
                    'msg' => '添加成功'
                ]);
            }else {
                throw new LoginException([
                    'msg' => '添加失败'
                ]);
            }
        }
        $this->assign('content', null);

        //获取更新操作的数据
        if(Request::instance()->param('id')) {
            $this->assign('content', ArtModel::get(Request::instance()->param('id')));
        }
        return view();
    }

    //封面图上传
    public function receiveImg() {
        $file = request()->file('file');
        $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            $dir =   '/uploads/'.$info->getSaveName();
            throw new SuccessMessage([
                'msg' => '上传成功',
                'data' => $dir
            ]);
        }else{
            // 上传失败获取错误信息
            throw new UploadImgException([
                'msg' =>$file->getError()
            ]);
        }
    }

    //编辑器图片处理类
    public function editIng() {
        $file = request()->file('file');
        $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            $dir =   '/uploads/'.$info->getSaveName();
            throw new SuccessMessage([
                'msg' => '上传成功',
                'data'=>$dir,
            ]);
        }else{
            // 上传失败获取错误信息
            throw new UploadImgException([
                'msg' =>$file->getError()
            ]);
        }
    }

    //素材列表
    public function artList() {
        if(Request::instance()->post()) {
            $title = Request::instance()->post('title') ? Request::instance()->post('title') : null;
            $type = Request::instance()->post('type') ? $seachData['type'] = Request::instance()->post('type') : null;

            $seachData['title'] = array('like', "%".$title."%");
            $seachData['status'] = 1;
            $list = ArtModel::where($seachData)->select();

            if(count($list) == 0) {
                throw new LoginException([
                    'msg' => '没有找到数据,换个条件试试  :)'
                ]);
            }else {
                throw new SuccessMessage([
                    'data' => $list
                ]);
            }
        }else {
            $list = ArtModel::where(array('status' => 1))->paginate(20);
        }

        $this->assign('list', $list);


        return view();
    }

    //列表页的手机预览
    public function preview() {
        if(Request::instance()->post()) {
            $data = Request::instance()->post();

            $res = ArtModel::get($data['id']);
            if($res) {
                throw new SuccessMessage([
                    'msg' => '查找成功',
                    'data' => $res
                ]);
            }else {
                throw new LoginException([
                    'msg' => '查找失败'
                ]);
            }
        }
    }

    //删除推文
    public function del() {
        if(Request::instance()->post()) {
            $id = intval(Request::instance()->post('del_id'));
            if(ArtModel::where(array('id' => $id))->update(array('status' => 0))) {
                throw new SuccessMessage([
                    'msg' => '删除成功'
                ]);
            }else {
                throw new LoginException([
                    'msg' => '删除失败'
                ]);
            }
        }
    }

    //返回ajax信息
    public function returnAjax($bool) {
        if($bool) {
            throw new SuccessMessage([
                'msg' => '添加成功'
            ]);
        }else {
            throw new LoginException([
                'msg' => '添加失败'
            ]);
        }
    }
}